package com.zhongxiangspace.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.zhongxiangspace.entity.County;
import com.zhongxiangspace.entity.House;
import com.zhongxiangspace.entity.QueryAnalyzer;
import com.zhongxiangspace.entity.VO.OfficeBHVO;
import com.zhongxiangspace.entity.VO.QueryAnalyzerOffice;
import com.zhongxiangspace.enums.AreaExtentEnum;
import com.zhongxiangspace.enums.DecorationEnum;
import com.zhongxiangspace.enums.PagesEnum;
import com.zhongxiangspace.enums.PriceExtenEnum;
import com.zhongxiangspace.result.ExceptionResult;
import com.zhongxiangspace.result.PageResult;
import com.zhongxiangspace.result.Result;
import com.zhongxiangspace.service.AdvertService;
import com.zhongxiangspace.service.ArticleService;
import com.zhongxiangspace.service.BannerService;
import com.zhongxiangspace.service.CountyService;
import com.zhongxiangspace.service.HouseService;
import com.zhongxiangspace.service.OfficeBuildingService;
import com.zhongxiangspace.service.StationBranchService;
import com.zhongxiangspace.tools.AnalyzerConditionTools;
import com.zhongxiangspace.tools.ReadProperties;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class OfficeListController {
    @Autowired
    private ReadProperties readProperties;
    @Autowired
    private BannerService bannerService;
    @Autowired
    private CountyService countyService;
    @Autowired
    private OfficeBuildingService officeBuildingService;
    @Autowired
    private AdvertService advertService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private StationBranchService stationBranchService;
    @Autowired
    private ArticleService articleService;

    @GetMapping({"lists/h1{value}"})
    public String offficeList(@PathVariable("value") String value, Model model) {
        return officeList(value, "pv1", "", model);
    }

    @GetMapping({"lists/h1{value}/{page}"})
    public String offficeList(@PathVariable("value") String value, @PathVariable("page") String page, Model model) {
        return officeList(value, page, "", model);
    }

    @GetMapping({"lists/h1{value}/{page}/{keyword}"})
    public String officeList(@PathVariable("value") String value, @PathVariable("page") String page, @PathVariable("keyword") String keyword, Model model) {
        Map<String, Object> map = new HashMap();
        if ((page == "") || (page.length() == 0)) {
            page = "pv1";
        }
        model.addAttribute("keyword", keyword);
        Integer pageSize = PagesEnum.getEnumObject(page).getIndex();
        map.put("url", value);
        String regions = AnalyzerConditionTools.getCondition(value, "r");
        map.put("regions", regions);
        if (!regions.isEmpty()) {
            County localCounty1 = this.countyService.queryOne(Integer.valueOf(regions.substring(1, regions.length())));
        }
        String area = AnalyzerConditionTools.getCondition(value, "a");
        String employees = AnalyzerConditionTools.getCondition(value, "e");
        if (!area.isEmpty()) {
            map.put("area", area);
        } else {
            map.put("area", employees);
        }
        String price = AnalyzerConditionTools.getCondition(value, "p");
        map.put("price", price);
        String decorate = AnalyzerConditionTools.getCondition(value, "d");
        map.put("decorate", decorate);

        QueryAnalyzer tradeAreaQueryAnalyzer = new QueryAnalyzer();
        tradeAreaQueryAnalyzer.setShowCode(Integer.valueOf(1));
        tradeAreaQueryAnalyzer.setHotCode(Integer.valueOf(1));
        List<Map<String, Object>> tradeAreaVOS = this.countyService.queryTradeAreaByKeyword(tradeAreaQueryAnalyzer);
        model.addAttribute("tradeArea", tradeAreaVOS);
        String tradeArea_id = AnalyzerConditionTools.getCondition(value, "t");
        if (!tradeArea_id.isEmpty()) {
            County localCounty2 = this.countyService.queryOne(Integer.valueOf(tradeArea_id.substring(1, tradeArea_id.length())));
        }
        map.put("tradeArea_id", tradeArea_id);

        QueryAnalyzer countQueryAnalyzer = new QueryAnalyzer();
        countQueryAnalyzer.setShowCode(Integer.valueOf(1));
        countQueryAnalyzer.setType("0");
        List<County> countyList = this.countyService.queryByKeyword(countQueryAnalyzer);
        map.put("county", countyList);

        QueryAnalyzerOffice queryAnalyzerOffice = new QueryAnalyzerOffice();
        queryAnalyzerOffice.setPage(pageSize);
        if (!regions.isEmpty()) {
            queryAnalyzerOffice.setCounty_id(Integer.valueOf(regions.substring(1, regions.length())));
        }
        if (!tradeArea_id.isEmpty()) {
            queryAnalyzerOffice.setTradeArea_id(Integer.valueOf(tradeArea_id.substring(1, tradeArea_id.length())));
        }
        if (!decorate.isEmpty()) {
            queryAnalyzerOffice.setDecorate(String.valueOf(DecorationEnum.getEnumObject(decorate).getValue()));
        }
        if (!area.isEmpty()) {
            queryAnalyzerOffice.setMinArea(AreaExtentEnum.getEnumObject(area).getMin());
            queryAnalyzerOffice.setMaxArea(AreaExtentEnum.getEnumObject(area).getMax());
        }
        if (!price.isEmpty()) {
            queryAnalyzerOffice.setMinPrice(PriceExtenEnum.getEnumObject(price).getMin());
            queryAnalyzerOffice.setMaxPrice(PriceExtenEnum.getEnumObject(price).getMax());
        }
        if ((keyword != null) && (!keyword.isEmpty())) {
            queryAnalyzerOffice.setKeyword(keyword);
        }
        String gson = new Gson().toJson(queryAnalyzerOffice);
        map.put("queryAnalyzerOffice", gson);

        model.addAttribute("data", map);
        return "list/office_list_mobile";
    }

    @ResponseBody
    @PostMapping({"/lists/ajaxOfficeData"})
    public Result ajxaData(QueryAnalyzerOffice queryAnalyzerOffice) {
        try {
            PageHelper.startPage(queryAnalyzerOffice.getPage().intValue(), 8, "office_buildings.hotCode DESC");
            Page<OfficeBHVO> officeBHVOPage = (Page) this.officeBuildingService.queryOfficeBH(queryAnalyzerOffice);
            List<OfficeBHVO> officeBHVOList = officeBHVOPage.getResult();
            for (OfficeBHVO office : officeBHVOList) {
                queryAnalyzerOffice.setBuilding_id(office.getBuilding_id());
                queryAnalyzerOffice.setDistinct("coveredArea");
                List<House> houseList = this.houseService.queryBHList(queryAnalyzerOffice);
                office.setHouses(houseList);
            }
            return new PageResult(officeBHVOPage.getPageNum(), officeBHVOPage.getPageSize(), officeBHVOPage.getTotal(), officeBHVOPage.getPages(), officeBHVOPage.getResult(), true);
        } catch (Exception e) {
            e.printStackTrace();
            return new ExceptionResult(e);
        }
    }
}
